﻿<div id="divFacebookLogin">
    @if (Request.IsAuthenticated)
    {
        @Html.Partial("LogoutPartialView")
    }
    else
    {
        <a href="#" onclick="fblogin();return false;"><img src="/images/facebook_login.png"></a>
        <div id="fb-root"></div>
        <script>
            window.fbAsyncInit = function () {
                FB.init({
                    appId: '440322405989724', // App ID
                    channelUrl: '//localhost/channel.html', // Channel File
                    status: true, // check login status
                    cookie: true, // enable cookies to allow the server to access the session
                    xfbml: true  // parse XFBML
                });

                FB.Event.subscribe('auth.login', function (response) {
                    if (response.authResponse) {
                        $.post('@Url.Action("FacebookLogin", "Account")',
                            {
                                accessToken: response.authResponse.accessToken,
                                userId: response.authResponse.userID,
                                expiresIn: response.authResponse.expiresIn
                            },
                             function () {
                                 showLogoff();
                             }
                        );

                    } else {
                        // user has not auth'd your app, or is not logged into Facebook
                    }
                });

                FB.Event.subscribe('auth.statusChange ', function (response) {
                    console.log("status change : " + JSON.stringify(response));
                });
            };

            // Load the SDK Asynchronously
            (function (d) {
                var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                if (d.getElementById(id)) { return; }
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/en_US/all.js";
                ref.parentNode.insertBefore(js, ref);
            }(document));

            function fblogin() {
                FB.login(function (response) {
                    console.log(JSON.stringify(response));
                    if (response.authResponse) {
                        $.post('@Url.Action("FacebookLogin", "Account")',
                            {
                                accessToken: response.authResponse.accessToken,
                                userId: response.authResponse.userID,
                                expiresIn: response.authResponse.expiresIn
                            }, function () {
                                showLogoff();
                            }
                        );

                    } else {
                        // user has not auth'd your app, or is not logged into Facebook
                    }
                }, { scope: 'email, user_checkins' });
            }

            function showLogoff() {
                $("div#divFacebookLogin").load('@Url.Action("LogoutPartialView", "Account")');
            }
        </script>
    }
</div>
